package com.example.shouybl.ui;


import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.R;
import com.example.shouybl.bean.GiftDetailBase;
import com.example.shouybl.bean.GiftDetInfo;
import com.example.shouybl.constant.Path;
import com.example.shouybl.fragment.GiftMobilFragment;
import com.example.shouybl.utils.ImageLoaderUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

import cz.msebera.android.httpclient.Header;

public class GiftDetailActivity extends BaseActivity {

    private GiftDetInfo detail=new GiftDetInfo();
    private AsyncHttpClient client=new AsyncHttpClient();
    private android.widget.TextView gametypetv;
    private android.widget.TextView tvdetailsize;
    private android.widget.Button btndetaildl;
    private android.widget.TextView tvdetailcontent;
    private android.widget.TextView tvdetailhowget;
    private android.widget.Button btndetailget;
    private ImageView img;
    private TextView tvConsume;
    private TextView tvName;
    private ProgressBar pbar;
    private TextView tvRemain;
    private TextView tvSETime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gift_detail);
        initView();
        initData();
    }

    private void initData() {
        client.post(Path.PATH_GIFT_DETAIL, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<GiftDetailBase> typeReference=new TypeReference<GiftDetailBase>(){};
                GiftDetailBase base= JSONObject.parseObject(responseString,GiftDetailBase.class);
                if (base!=null && base.getState().equals("success")){
                detail=base.getInfo();
                }
                ImageLoader.getInstance().displayImage(detail.getIcon(),img, ImageLoaderUtil.getDefaultOptions());
                tvConsume.setText(detail.getConsume() + "Q币");
                tvName.setText(detail.getName());
                pbar.setProgress((Integer.parseInt(detail.getRemain()) / Integer.parseInt(detail.getTotal())) * 100);
                tvRemain.setText(detail.getRemain() + "/" + detail.getTotal());
                tvSETime.setText(detail.getStime()+"--"+detail.getEtime());
                tvdetailhowget.setText(detail.getHowget());
                tvdetailcontent.setText(detail.getContent());
                tvdetailsize.setText("大小:"+detail.getSize());
                gametypetv.setText("类型:"+detail.getGame_type());
            }
        });
    }

    private RequestParams getParams() {
        RequestParams params=new RequestParams();
        params.add("id",getIntent().getStringExtra(GiftMobilFragment.INTENT_NAME));
        params.add("compare", "78fc45f6f%2C7%2C9");
        return params;
    }

    private void initView() {
        img = (ImageView) findViewById(R.id.iv_detail_icon);
        tvConsume = (TextView) findViewById(R.id.tv_detail_consume);
        tvName = (TextView) findViewById(R.id.tv_detail_name);
        pbar = (ProgressBar) findViewById(R.id.progressBar);
        tvRemain = (TextView) findViewById(R.id.tv_detail_remain);
        tvSETime = (TextView) findViewById(R.id.tv_stime_etime);
        this.btndetailget = (Button) findViewById(R.id.btn_detail_get);
        this.tvdetailhowget = (TextView) findViewById(R.id.tv_detail_howget);
        this.tvdetailcontent = (TextView) findViewById(R.id.tv_detail_content);
        this.btndetaildl = (Button) findViewById(R.id.btn_detail_dl);
        this.tvdetailsize = (TextView) findViewById(R.id.tv_detail_size);
        this.gametypetv = (TextView) findViewById(R.id.game_type_tv);
    }
}
